#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set port [lindex $argv 2]
set passwd [lindex $argv 3]
set name [lindex $argv 4]
set upasswd [lindex $argv 5]
if { $argc != 6 } {
puts "Usage: expect login.exp ip user passwd"
exit 1
}

puts $name
puts $upasswd

set timeout 30
spawn ssh $user@$ip
expect {    
    "yes/no" {send "yes\r";exp_continue;}
    "password:" {send "$passwd\r"}
}
expect "#" {send "useradd ${name}\r"}
expect "#" {
    send "passwd ${name}\r"
    expect {    
        "新的 密码：" {send "${upasswd}\r"}
    }
    expect {    
        "重新输入新的 密码：" {send "${upasswd}\r"}
    }
}
expect "#" {send "sed -i '100a\\${name}    ALL=(ALL)       ALL' /etc/sudoers\r"}
# expect "#" {send "sed -i '/PermitRootLogin /c PermitRootLogin no' /etc/ssh/sshd_config\r"}
expect "#" {send "service sshd restart\r"}
expect "#" {send "exit\r"}
interact

# sed -i '100a\externalteam    ALL=(ALL)       ALL' /etc/sudoers
# sed -i '/PermitRootLogin /c PermitRootLogin no' /etc/ssh/sshd_config
# service sshd restart